package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
@Slf4j
public class DishServiceImpl implements DishService {


    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;


    @Override
    public Page<Dish> findByPage(String name, Integer pageNum, Integer pageSize) {
        //1. 在菜品表中根据name分页查询
        //1-1 准备分页对象
        Page<Dish> page = new Page<>(pageNum, pageSize);

        //1-2 准备封装查询条件
        //StringUtils.isNotEmpty(name) 判断name是否为非空字符串
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name), Dish::getName, name);

        //1-3 执行菜品列表分页查询
        page = dishMapper.selectPage(page, wrapper);

        //2 遍历菜品列表
        List<Dish> dishList = page.getRecords();
        if (CollectionUtil.isNotEmpty(dishList)) {
            for (Dish dish : dishList) {
                //3. 根据菜品的分类id从分类表中查询分类信息(名称)
                Category category = categoryMapper.selectById(dish.getCategoryId());
                dish.setCategoryName(category.getName());

                //4 根据菜品id从口味表查询当前菜品口味列表
/*                LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(DishFlavor::getDishId,dish.getId());
                List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(wrapper1);
                dish.setFlavors(dishFlavorList);*/

            }
        }

        return page;
    }

    //保存  新增菜品
    @Override
    public void save(Dish dish) {
        //1 向我们的dish表保存基本信息 ，{mybatis plus会自动返回主键}
        dishMapper.insert(dish);
        log.info("保存之后的菜品主键:{}", dish.getId());

        //2 遍历前端传入的口味列表信息
        List<DishFlavor> flavorList = dish.getFlavors();
        if (CollectionUtil.isNotEmpty(flavorList)) {
            for (DishFlavor dishFlavor : flavorList) {
                //3 为口味设置所属菜品id
                dishFlavor.setDishId(dish.getId());
                //4 保存口味
                dishFlavorMapper.insert(dishFlavor);
            }
        }


    }

    @Override
    public Dish findById(Long id) {
        //1 从菜品表根据id查询
        Dish dish = dishMapper.selectById(id);
        //2 根据菜品的分类id从分类表中查询分类的信息 (名称)
        Category category = categoryMapper.selectById(dish.getCategoryId());
        dish.setCategoryName(category.getName());
        //3 根据菜品id从口味表查询当前菜品口味列表
        LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(wrapper1);
        dish.setFlavors(dishFlavorList);


        return dish;
    }

    @Override
    public void update(Dish dish) {
        // 1 根据菜品id直接更新菜品基本信息
        dishMapper.updateById(dish);

        //2 删除口味表中当前菜品的相关信息
        //delete from dish_flavor where dish = #{}
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId, dish.getId());
        dishFlavorMapper.delete(wrapper);

        //3 将修改之后口味列表重新保存到口味表中
        List<DishFlavor> flavors = dish.getFlavors();
        if (CollectionUtil.isNotEmpty(flavors)) {
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dish.getId());//设置菜品id
                dishFlavorMapper.insert(flavor);
            }
        }

    }

    @Override
    public void updateById(Integer status, List<String> ids) {
        for (String id : ids) {
            LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Dish::getId, id);
            Dish dish = new Dish();
            dish.setStatus(status);
            dishMapper.update(dish, wrapper);

        }
    }

    @Override
    public Boolean delete(List<String> ids) {
        String status = "";
        for (String id : ids) {
            Dish dish = dishMapper.selectById(id);
            status += dish.getStatus();

        }
        if (!status.contains("1")) {
            dishMapper.deleteBatchIds(ids);
            LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
            for (String id : ids) {
                wrapper.eq(DishFlavor::getId, id);
                //        Long dishId = dishFlavorMapper.selectById(id).getDishId();
                LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(DishFlavor::getDishId, id);
                dishFlavorMapper.delete(queryWrapper);
                return true;
            }
        }
            return false;
    }

    @Override
    public List<Dish> findByCategoryId(Long categoryId) {
        //select * from dish where categoryId = #{categoryId} and status = 1
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId,categoryId)
                .eq(Dish::getStatus,1);//判断上级为0

        return  dishMapper.selectList(wrapper);

    }

    @Override
    public List<Dish> findList(Long categoryId, Integer status) {

        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId,categoryId).eq(Dish::getStatus,status);
        List<Dish> dishList = dishMapper.selectList(wrapper);
        if (CollectionUtil.isNotEmpty(dishList)) {
            for (Dish dish : dishList) {
                //得到每一个菜品，根据菜品的id从口味表中找到当前菜品的口味列表
                LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(DishFlavor::getDishId,dish.getId());
                List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(wrapper1);
                dish.setFlavors(dishFlavorList);
            }
        }

        return dishList;
    }

    //模糊名字的查询
    @Override
    public List<Dish> findByName(String name) {

        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Dish::getName,name);
        List<Dish> dishList = dishMapper.selectList(wrapper);

        return dishList;
    }
}
